package com.atguigu.jinx.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.atguigu.jinx.entity.vo.*;
import com.atguigu.jinx.enums.SmsTemplateStatus;
import com.atguigu.jinx.service.IJobTaskService;
import com.atguigu.jinx.utils.R;
import com.google.common.util.concurrent.RateLimiter;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Description;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.atguigu.jinx.utils.NameConstants.SMS_SEND_FAILED;
import static com.atguigu.jinx.utils.NameConstants.SMS_SEND_SUCCESS;

/**
 * 登陆页面Portal
 *
 * @author kuangqi
 * @email stevekwong@126.com
 * @date 2025/8/7
 */
@RestController
@CrossOrigin
@Validated
@RequestMapping("/jinx/portal")
@RequiredArgsConstructor
public class PortalController {


    /**
     * 岗位展示
     */
    private final IJobTaskService iJobTaskService;

    /**
     * 岗位展示列表信息
     */
    @Description("岗位展示列表信息")
    @GetMapping("/portalJobList")
    public R list() {
        return R.ok().put("jobs", iJobTaskService.getPortalJobListVO());
    }

    /**
     * 邮箱动态码发送(登录)
     *
     * @param email 邮箱
     * @return 校验结果
     */
    @Description("邮箱动态码发送(登录)")
    @GetMapping({"/sendEmailDynamicCode/{emailNum}"})
    public R sendEmailDynamicCode(@PathVariable(value = "emailNum")
                                  @Email(message = "邮箱格式不正确")
                                  String email) {
        return iJobTaskService.sendEmailDynamicCode(email)
                ? R.ok(SMS_SEND_SUCCESS)
                : R.ok(SMS_SEND_FAILED);
    }

    /**
     * 手机动态码发送(登录)
     *
     * @param phone 手机号码
     * @return 校验结果
     */
    @Description("手机动态码发送(登录)")
    @GetMapping({"/sendPhoneDynamicCode/{phoneNum}"})
    public R sendPhoneDynamicCode(@RequestParam(value = "phoneNum", required = false)
                                  @Pattern(regexp = "^(1[3-9]\\d{9}|0\\d{9,10})$", message = "手机号格式不正确")
                                  String phone) {
        return iJobTaskService.sendPhoneDynamicCode(phone, SmsTemplateStatus.LOGIN_TEMPLATE)
                ? R.ok(SMS_SEND_SUCCESS) : R.ok(SMS_SEND_FAILED);
    }

    /**
     * 手机登录校验
     *
     * @param loginPhoneVO 手机+验证码
     * @return 1.校验并登录 2.校验未注册 3.校验失败
     */
    @Description("手机登录校验")
    @PostMapping("/phoneDynamicCodeLogin")
    public R phoneDynamicCodeLogin(@RequestBody LoginPhoneVO loginPhoneVO) {
        LoginResultVO resultVO = iJobTaskService.phoneDynamicCodeLogin(loginPhoneVO);
        return R.ok().put("result", resultVO);
    }

    /**
     * 邮箱登录校验
     *
     * @param loginPhoneVO 邮箱+验证码
     * @return 1.校验并登录 2.校验未注册 3.校验失败
     */
    @Description("邮箱登录校验")
    @PostMapping("/emailDynamicCodeLogin")
    public R emailDynamicCodeLogin(@RequestBody LoginPhoneVO loginPhoneVO) {
        LoginResultVO resultVO = iJobTaskService.phoneDynamicCodeLogin(loginPhoneVO);
        return R.ok().put("result", resultVO);
    }

    /**
     * 账号密码登录校验
     *
     * @param loginUPVO 手机+验证码
     * @return 1.校验并登录 2.校验未注册 3.校验失败
     */
    @Description("账号密码登录校验")
    @PostMapping("/passWordLogin")
    @SentinelResource(value = "testResource",blockHandler = "handle")
    public R passWordLogin(@RequestBody @Validated LoginUPVO loginUPVO) {
        LoginResultVO resultVO = iJobTaskService.passWordLogin(loginUPVO);
        return R.ok().put("result", resultVO);
    }


    /**
     * 用户注册
     *
     * @param userVO 用户注册信息
     * @return 用户注册的结果
     */
    @Description("用户注册")
    @PostMapping("/userSignUp")
    public R userSingUp(@RequestBody @Validated UserVO userVO) {
        boolean registrationResult = iJobTaskService.sendLoginInfo(userVO);
        return R.ok().put("result", registrationResult);
    }


    /**
     * 校验是否为系统用户并发送验证码
     * 前端去判断是手机号还是邮箱号
     *
     * @param phone 手机号
     * @param email 邮箱号
     * @return 校验该用户使用的手机号或者邮箱号是否已注册
     */
    @Description("校验是否为系统用户")
    @GetMapping("/forgetPasswordValid")
    public R forgetPasswordValid(@RequestParam(value = "phoneNum", required = false)
                                 @Pattern(regexp = "^(1[3-9]\\d{9}|0\\d{9,10})$", message = "手机号格式不正确")
                                 String phone,
                                 @RequestParam(value = "emailNum", required = false)
                                 @Email(message = "邮箱格式不正确")
                                 String email) {
            boolean retrieveValidResult = iJobTaskService.forgetPasswordValid(phone, email);
            return R.ok().put("result", retrieveValidResult);
    }


    /**
     * 用户忘记密码校验验证码
     *
     * @param loginPhoneVO 1.邮箱 2.手机号 3.验证码
     * @return (false 验证不通过 ; true - > 发送到重置密码的页面)
     */
    @Description("用户忘记密码校验验证码")
    @PostMapping("/forgetPassword")
    public R forgetPassword(@RequestBody @Validated LoginPhoneVO loginPhoneVO) {
        boolean retrieveResult = iJobTaskService.checkSmsCode(loginPhoneVO,SmsTemplateStatus.FORGET_TEMPLATE);
        return R.ok().put("result", retrieveResult);
    }


    /**
     * 重置密码
     *
     * @param loginUPVO 1.邮箱 2.手机号 3.验证码
     * @return 重置密码的结果
     */
    @Description("重置密码")
    @PostMapping("/resetPassword")
    public R resetPassword(@RequestBody @Validated ResetPasswordVO loginUPVO) {
        boolean retrieveResult = iJobTaskService.resetPassword(loginUPVO);
        return R.ok().put("result", retrieveResult);
    }
}
